package com.hdey.controller.heng;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdey.pojo.heng.TbUscRole;
import com.hdey.pojo.heng.TbUscUser;
import com.hdey.service.heng.RoleService;
import com.hdey.service.heng.UserService;
import com.hdey.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserService service;
    @Resource
    private RoleService roleService;

    private String msg;

    /**
     * 结合模糊查询以及分页查询
     * @param page
     * @param limit
     * @param userName 用户姓名
     * @param roleId  角色编码
     * @return
     */
    @GetMapping("/search")
    @ResponseBody
    public Result search(Integer page, Integer limit, String userName, Integer roleId){
        System.out.println("Controller:"+roleId);
        Page<TbUscUser> list=service.showAll(page,limit,userName,roleId);
        return Result.success(list);
    }

    /**
     * 查询角色表，将数据填充至下拉框
     * @return
     */
    @RequestMapping("/searchRole")
    public Result searchRole(){
        List<TbUscRole> roles=roleService.showAll();
        return Result.success(roles);
    }

    /**
     * 添加用户
     * @param tbUscUser
     * @return
     */
    @PostMapping("/add")
    public Result add(TbUscUser tbUscUser){
        tbUscUser.setPassword(new Md5Hash(tbUscUser.getPassword(), null , 1).toString());

        try {
            service.add(tbUscUser);
            msg="添加用户成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="添加用户失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }

    @PostMapping("/showByloginName")
    public Result showByloginName(String loginName){
        TbUscUser tbUscUser=service.showByloginName(loginName);
        return Result.success(tbUscUser);
    }

    @PostMapping("/del")
    public Result del(Integer userId){
        try {
            service.del(userId);
            msg="删除用户成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="删除用户失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }

    @PostMapping("/showByUserId")
    public Result showByUserId(Integer userId){
        System.out.println("userId:"+userId);
        TbUscUser tbUscUser=service.showByUserId(userId);
        return Result.success(tbUscUser);
    }

    @PostMapping("/update")
    public Result update(TbUscUser tbUscUser){
        System.out.println("进入controller");
        System.out.println(tbUscUser);
        try {
            service.update(tbUscUser);
            msg="修改用户成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="修改用户失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }

    @PostMapping("/showroleName")
    public String showroleName(Integer roleId){
        String name=roleService.showroleName(roleId);
        return name;
    }

}
